<template>
	<div class="system-area-container">
		<el-dialog title="修改区划" v-model="isShowDialog" >
			<el-form ref="editAreaFormRef" :model="formData" :rules="formRules" label-width="80px">
				<el-row :gutter="35">
					<el-col>
						<el-form-item label="上级区划">
							<el-cascader
								placeholder="请选择上级区划（可选填）"
								:key="areaModelKey"
								v-model="formData.parentId"
								:options="areas"
								:props="areaProps"
								class="w100"
								clearable>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="区划代码" prop="id">
							<el-input v-model="formData.id" placeholder="请输入区划代码" readonly></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="区划名称" prop="areaName">
							<el-input v-model="formData.areaName" placeholder="请输入区划名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="区划等级" prop="areaLevel">
							<el-select v-model="formData.areaLevel" placeholder="请选择区划等级" clearable class="w100">
								<el-option label="省级" :value="1"></el-option>
								<el-option label="地级" :value="2"></el-option>
								<el-option label="县级" :value="3"></el-option>
								<el-option label="乡级" :value="4"></el-option>
								<el-option label="村级" :value="5"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="区划状态" prop="status">
							<el-select v-model="formData.status" placeholder="请选择区划状态" clearable class="w100">
								<el-option label="正常" value="0"></el-option>
								<el-option label="停用" value="1"></el-option>
								<el-option label="删除" value="2"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="区划简称" prop="shortName">
							<el-input v-model="formData.shortName" placeholder="请输入区划简称" :disabled="formData.areaLevel != 1" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="省会城市" prop="capitalCity">
							<el-input v-model="formData.capitalCity" placeholder="请输入省会城市" :disabled="formData.areaLevel != 1" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="邮政编码" prop="postCode">
							<el-input v-model="formData.postCode" placeholder="请输入邮政编码" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="区划排序" prop="orderNum">
							<el-input-number v-model="formData.orderNum" :min="0" :max="999" clearable class="w100"></el-input-number>
						</el-form-item>
					</el-col>
					<el-col>
						<el-form-item label="备注">
							<el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel">取 消</el-button>
					<el-button type="primary" @click="onSubmit">保 存</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { getAreaList, editArea } from '/@/api/system/area/index';
import { lineToTreeData } from '/@/utils/commonFunction';
export default {
	name: 'area/edit',
	setup(props, context) {
		const editAreaFormRef = ref();
		const rootId = '100000000000';
		const state = reactive({
			isShowDialog: false,
			areas: [],
			areaModelKey: 1,
			formData: {},
			formRules : {
				areaName: { required: true, message: '区划名称不能为空', trigger: 'blur' },
				id: { required: true, message: '区划代码不能为空', trigger: 'blur' },
				status: { required: true, message: '区划状态不能为空', trigger: 'change' },
				orderNum: { required: true, message: '区划排序不能为空', trigger: 'blur' }
			},
			areaProps: {
				expandTrigger: 'hover', 
				checkStrictly: true, 
				emitPath: false,
				value: 'id', 
				label: 'areaName', 
				children: 'children',
				lazy: true,
				lazyLoad: (node, resolve) => {
					if(node.value){
						getAreaList({'parentId': node.value}).then((res) => {
							if(res.code == 0){
								resolve(res.data);
							}
						});
					}
				}
			}
		});
		//初始化上级区划级联
		const loadAreas = async (parentId) => {
			await getAreaList({'parentId': parentId}).then((res) => {
				if(res.code == 0){
					state.areas = [...state.areas, ...res.data];
				}
			});
		}
		// 打开弹窗
		const openDialog = async (row: any) => {
			state.isShowDialog = true;
			await loadAreas(rootId);
			if(row){
				state.formData = Object.assign({}, row);
				//处理上级区划级联选择
				let ancestors = row.ancestors.split(',');
				
				for(let i = 0; i < ancestors.length; i++){
					await loadAreas(ancestors[i]);
				}
				state.areas = lineToTreeData(state.areas);
				state.areaModelKey++;//改变modelkey值，组件重新渲染，实现回填功能
			}
		};
		// 关闭弹窗
		const closeDialog = (row?: object) => {
			state.isShowDialog = false;
			for(var key in state.formData) {
				state.formData[key] = undefined;
			}
			state.areas = [];
			editAreaFormRef.value.resetFields();
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 保存
		const onSubmit = () => {
			editAreaFormRef.value.validate(valid => {
				if(valid){
					let params = {
						id: state.formData.id,
						parentId: state.formData.parentId,
						areaName: state.formData.areaName,
						areaLevel: state.formData.areaLevel,
						status: state.formData.status,
						shortName: state.formData.shortName,
						capitalCity: state.formData.capitalCity,
						postCode: state.formData.postCode,
						orderNum: state.formData.orderNum,
						remark: state.formData.remark
					}
					editArea(params).then((res) => {
						if(res.code === 0){
							ElMessage.success('保存成功');
							context.emit('submit');
							closeDialog(); // 关闭弹窗
						}
					});
				}
			});
		};
		
		return {
			editAreaFormRef,
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			...toRefs(state),
		};
	},
};
</script>
